package com.example.myapplication.view

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.Window
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.example.myapplication.R
import com.example.myapplication.utils.RichTextUtils

class CommonDialog(
    context: Context,
    private val config: Config
) : Dialog(context, R.style.CommonDialog) {

    data class Config(
        val icon: Drawable? = null,
        val title: String = "温馨提示",
        val content: String? = null,
        val positiveText: String,
        val negativeText: String? = null, // null表示单按钮模式
        var cancelable: Boolean = true,
        val onPositiveClick: (() -> Unit)? = null,
        val onNegativeClick: (() -> Unit)? = null,
        val wrapContent: Boolean = false
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.custom_dialog)

        findViewById<FrameLayout>(R.id.nsv).apply {
            if (config.wrapContent) {
                layoutParams =
                    LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT
                    )
            }
        }
        findViewById<ImageView>(R.id.ivIcon).apply {
            isVisible = config.icon != null
            config.icon?.let { setImageDrawable(it) }
        }

        findViewById<ImageView>(R.id.iv_close).apply {
            setOnClickListener {
                dismiss()
            }
            isVisible = config.cancelable == true
        }

        findViewById<TextView>(R.id.tvTitle).text = config.title
        val tvContent = findViewById<TextView>(R.id.tvContent)
        RichTextUtils.setRichTextWithCustomLink(tvContent, config.content)

        val btnPositive = findViewById<Button>(R.id.btnPositive).apply {
            text = config.positiveText
            setOnClickListener {
                config.onPositiveClick?.invoke()
                dismiss()
            }
        }

        val btnNegative = findViewById<Button>(R.id.btnNegative).apply {
            isVisible = config.negativeText != null
            text = config.negativeText
            setOnClickListener {
                config.onNegativeClick?.invoke()
                dismiss()
            }
        }

        if (config.negativeText == null) {
            btnPositive.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                marginStart = 0
            }
        }

        setCancelable(config.cancelable)
        setCanceledOnTouchOutside(config.cancelable)
        setOnCancelListener { config.onNegativeClick?.invoke() }
    }

    companion object {
        fun show(
            context: Context,
            config: Config,
            isCancelable: Boolean = false
        ): CommonDialog {
            return CommonDialog(context, config.apply {
                this.cancelable = isCancelable
            }).apply { show() }
        }
    }
}